package com.letian.info.service.impl;

import java.util.List;
import com.letian.common.utils.DateUtils;
import com.letian.info.domain.InfoArticle;
import com.letian.info.mapper.InfoArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.letian.info.mapper.InfoCommentMapper;
import com.letian.info.domain.InfoComment;
import com.letian.info.service.IInfoCommentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 评论信息Service业务层处理
 * 
 * @author bmz
 * @date 2023-04-05
 */
@Service
public class InfoCommentServiceImpl implements IInfoCommentService 
{
    @Autowired
    private InfoCommentMapper infoCommentMapper;

    @Autowired
    private InfoArticleMapper infoArticleMapper;

    /**
     * 查询评论信息
     * 
     * @param commentId 评论信息主键
     * @return 评论信息
     */
    @Override
    public InfoComment selectInfoCommentByCommentId(Long commentId)
    {
        return infoCommentMapper.selectInfoCommentByCommentId(commentId);
    }

    /**
     * 通过回复ID查询评论列表
     * @param replyId
     * @return
     */
    @Override
    public List<InfoComment> selectInfoCommentListByReplyId(Long replyId) {
        return infoCommentMapper.selectInfoCommentListByReplyId(replyId);
    }

    /**
     * 查询评论信息列表
     * 
     * @param infoComment 评论信息
     * @return 评论信息
     */
    @Override
    public List<InfoComment> selectInfoCommentList(InfoComment infoComment)
    {
        return infoCommentMapper.selectInfoCommentList(infoComment);
    }

    /**
     * 新增评论信息
     * 
     * @param infoComment 评论信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInfoComment(InfoComment infoComment)
    {
        InfoArticle infoArticle = infoArticleMapper.selectInfoArticleByArticleId(infoComment.getObjectId());
        infoArticle.setCommentsCount(infoArticle.getCommentsCount()+1);
        infoArticleMapper.updateInfoArticle(infoArticle);

        infoComment.setCreateTime(DateUtils.getNowDate());
        return infoCommentMapper.insertInfoComment(infoComment);
    }

    /**
     * 修改评论信息
     * 
     * @param infoComment 评论信息
     * @return 结果
     */
    @Override
    public int updateInfoComment(InfoComment infoComment)
    {
        infoComment.setUpdateTime(DateUtils.getNowDate());
        return infoCommentMapper.updateInfoComment(infoComment);
    }

    /**
     * 批量删除评论信息
     * 
     * @param commentIds 需要删除的评论信息主键
     * @return 结果
     */
    @Override
    public int deleteInfoCommentByCommentIds(Long[] commentIds)
    {
        return infoCommentMapper.deleteInfoCommentByCommentIds(commentIds);
    }

    /**
     * 删除评论信息信息
     * 
     * @param commentId 评论信息主键
     * @return 结果
     */
    @Override
    public int deleteInfoCommentByCommentId(Long commentId)
    {
        InfoComment infoComment = infoCommentMapper.selectInfoCommentByCommentId(commentId);
        if (infoComment == null) {
            return 0;
        }
        InfoArticle infoArticle = infoArticleMapper.selectInfoArticleByArticleId(infoComment.getObjectId());
        infoArticle.setCommentsCount(infoArticle.getCommentsCount() - 1);
        infoArticleMapper.updateInfoArticle(infoArticle);

        return infoCommentMapper.deleteInfoCommentByCommentId(commentId);
    }
}
